﻿using AutoMapper;
using MediatR;
using MediatR.Wrappers;
using Purchase.ApiS.AllCation.Command.Quality;
using Purchase.Domian.Model;
using Purchase.ErrorCode;
using Purchase.Infrastructrue.Implenment;

namespace Purchase.ApiS.AllCation.Hanlder.Qualitypost
{
    public class DelQualityCommandHanlder : IRequestHandler<DelQualityCommand, ApiResult<int>>
    {
        DatabaseContext db;
        ILogger<DelQualityCommandHanlder> logger;
        IMapper mapper;

        public DelQualityCommandHanlder(DatabaseContext db, ILogger<DelQualityCommandHanlder> logger, IMapper mapper)
        {
            this.db = db;
            this.logger = logger;
            this.mapper = mapper;
        }
        /// <summary>
        /// 质检逻辑删除的方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(DelQualityCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var model = db.SqlSugar.Queryable<Quality>().First(x => x.QuaId == request.QuaId);
                model.IsDeleted = true;
                int count = db.SqlSugar.Updateable(model).ExecuteCommand();
                if (count > 0)
                {
                    result.Data = count;
                    result.Msg = "质检信息逻辑删除成功";
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Msg = "质检信息逻辑删除成功";
                    result.Code = ApiEume.Error;
                    result.Data = count;

                }
            }
            catch (Exception ex)
            {

            }
            return Task.FromResult(result);
        }
    }
}
